package com.ss.android.article.base.feature.favorite;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;

import com.bytedance.article.common.c.TTUtils;
import com.bytedance.frameworks.b.a.ComponentsManifest;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.ss.android.action.ArticleActionHelper;
import com.ss.android.article.base.feature.c.FeedAdapter;
import com.ss.android.article.base.feature.c.FeedItemBehavior;
import com.ss.android.article.base.feature.detail.presenter.DetailHelper;
import com.ss.android.article.base.feature.detail2.DetailIntentCreator;
import com.ss.android.article.base.feature.e.ArticleShareHelper;
import com.ss.android.article.base.feature.feed.activity.ArticleListFragment;
import com.ss.android.article.base.feature.feed.presenter.ArticleQueryHandler;
import com.ss.android.article.base.feature.feed.presenter.ArticleQueryObj;
import com.ss.android.article.base.feature.feed.presenter.ArticleQueryThread;
import com.ss.android.article.base.feature.feed.presenter.QueryCallback;
import com.ss.android.article.base.feature.model.CellRef;
import com.ss.android.article.base.utils.ArticleHelper;
import com.ss.android.article.browser.R;
import com.ss.android.common.lib.MobClickCombiner;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.model.ItemType;
import com.ss.android.model.BaseArticle;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Collection;
import java.util.List;

public final class PushHistoryListFragment extends ArticleListFragment implements FeedItemBehavior, QueryCallback {
    protected boolean a;
    private String b;
    private long c;
    private int d;
    private boolean e;
    private ArticleQueryHandler f;
    private int g;

    public PushHistoryListFragment() {
        this.c = -1;
        this.a = true;
        this.e = true;
        this.f = new ArticleQueryHandler(((QueryCallback) this));
    }

    static boolean a(PushHistoryListFragment arg1) {
        return arg1.mIsLoading;
    }

    public final void onStartQuery(ArticleQueryObj articleQueryObj) {
    }

    public final void onFinishQuery(boolean arg13, ArticleQueryObj articleQueryObj) {
        int v3_1;
        List v4;
        int v0_1;
        long v10 = 0;
        if ((this.isViewValid()) && articleQueryObj != null && articleQueryObj.b == this.d) {
            this.mFooter.hideFooterContent();
            this.mIsLoading = false;
            if (!arg13) {
                this.showNotify(this.getString(TTUtils.a(articleQueryObj.errorCode)), true);
                if (this.a) {
                    this.mPullRefreshList.onRefreshComplete();
                    this.a = false;
                }

                this.refreshList();
                this.mData.isEmpty();
            } else {
                List v0 = this.mAppData.a(articleQueryObj.q);
                if (this.a) {
                    this.mListData.h = v10;
                    this.mData.clear();
                    List v3 = android.support.design.a.a(this.mData, v0, true);
                    this.a = false;
                    if (!articleQueryObj.d) {
                        this.mListData.c = articleQueryObj.hasMore;
                    }

                    if (v3.isEmpty()) {
                        this.mListData.d = false;
                    }

                    v0_1 = (articleQueryObj.d) || !articleQueryObj.j || !articleQueryObj.v ? 0 : 1;
                    this.mAdsAppItem = articleQueryObj.mAdsAppItem;
                    if (articleQueryObj.mAdsAppItem != null) {
                        this.showNotify(this.mAdsAppItem, false);
                    }

                    this.mPullRefreshList.onRefreshComplete();
                    v4 = v3;
                    v3_1 = 1;
                } else {
                    if (!articleQueryObj.d) {
                        this.mListData.c = articleQueryObj.hasMore;
                    }

                    v0 = android.support.design.a.a(this.mData, v0, true);
                    if (!v0.isEmpty()) {
                        this.mListData.d = true;
                        v3_1 = 0;
                        v4 = v0;
                        v0_1 = 0;
                    } else if (articleQueryObj.d) {
                        this.mListData.d = false;
                        v3_1 = 0;
                        v4 = v0;
                        v0_1 = 0;
                    } else {
                        v3_1 = 0;
                        v4 = v0;
                        v0_1 = 0;
                    }
                }

                if (!v4.isEmpty()) {
                    this.mData.addAll(((Collection) v4));
                }

                if (articleQueryObj.lastCateBehotTime > v10) {
                    if (this.mListData.h > v10 && this.mListData.h <= articleQueryObj.lastCateBehotTime) {
                        this.mListData.h = Math.max(v10, this.mListData.h - 1);
                    } else {
                        this.mListData.h = articleQueryObj.lastCateBehotTime;
                    }
                } else {
                    this.mListData.h = Math.max(v10, this.mListData.h - 1);
                }

                this.refreshList();
                if (!this.mData.isEmpty() && v3_1 != 0) {
                    this.mListView.setSelection(0);
                }

                if (v0_1 == 0) {
                    return;
                }

                if (!NetworkUtils.isNetworkAvailable(this.mContext)) {
                    return;
                }

                this.a = true;
                this.queryData();
            }
        }
    }

    public final int contextType() {
        return 0;
    }

    public final void doHandleItemClick(int arg7, CellRef arg8, View arg9, boolean arg10, boolean arg11) {
        int v5 = 9;
        if (!this.mData.isEmpty()) {
            FragmentActivity v1 = this.getActivity();
            if (v1 != null && arg8 != null) {
                this.mListData.b = arg7;
                this.mListData.a = this.mData;
                this.mAppData.a(this.mListData, v5, null);
                this.g = 1;
                ArticleHelper.a = true;
                Intent v2 = new Intent();
                v2.putExtra("tag", this.b);
                v2.putExtra("list_type", v5);
                v2.putExtra("view_comments", arg10);
                v2.putExtra("show_write_comment_dialog", arg11);
                this.startActivityForResult(ComponentsManifest.findComponent(DetailIntentCreator.class).createNewDetailIntent(((Context) v1), v2.getExtras()), 110);
            }
        }
    }

    public final String getCategoryName() {
        return "push_history";
    }

    public final String getListCategory() {
        return null;
    }

    public final int getListType() {
        return 9;
    }

    public final boolean getRemoveDislikedItem() {
        return false;
    }

    public final int getViewLayout() {
        return R.layout.d6;
    }

    public final void onActivityCreated(Bundle arg12) {
        super.onActivityCreated(arg12);
        Bundle v0 = this.getArguments();
        if (v0 != null) {
            this.b = v0.getString("tag");
        }

        if (android.support.design.a.isTextEmpty(this.b)) {
            this.b = "news";
        }

        this.mArticleActionHelper = new ArticleActionHelper(this.mContext);
        this.mDetailHelper = new DetailHelper(this.getActivity(), ItemType.ARTICLE, this.f, this.mArticleActionHelper, "xiangping");
        this.mArticleShareHelper = new ArticleShareHelper(this.getActivity(), this.mArticleActionHelper, this.mDetailHelper, 201);
        this.mArticleShareHelper.mCategoryName = "__favor__";
        this.mAdapter = new FeedAdapter(this.getActivity(), this, this.mNetworkMonitor, this.mRootView,
                this, 9, this.mDiggAnimationView, this.mArticleActionHelper, this.mArticleShareHelper, "__favor__");
        this.registerLifeCycleMonitor(this.mAdapter);
        this.mAdapter.setListView(this.mListView);
        this.mListView.setRecyclerListener(this.mAdapter);
        this.mListView.setAdapter(this.mAdapter);
        this.mPullRefreshList.setMode(Mode.DISABLED);
        this.mPullRefreshList.setGravity(48);
        this.mPullRefreshList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {
            public final void onPullDownToRefresh(PullToRefreshBase toRefreshBase) {
                if (!PushHistoryListFragment.a(PushHistoryListFragment.this)) {
                    PushHistoryListFragment.this.a = true;
                    PushHistoryListFragment.this.queryData();
                }
            }
        });
        this.mIsLoading = false;
    }

    public final void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        this.c = -1;
    }

    public final View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        return super.onCreateView(inflater, container, bundle);
    }

    public final void onDeleteFavorClick(CellRef arg1) {
    }

    public final void onDestroyView() {
        super.onDestroyView();
    }

    public final void onEvent(String arg3) {
        MobClickCombiner.onEvent(this.getActivity(), "push_history", arg3);
    }

    public final void onEvent(String arg10, BaseArticle arg11) {
        long v6 = 0;
        if (arg11 != null && arg11.groupId > v6) {
            JSONObject v8 = new JSONObject();
            try {
                v8.put("item_id", arg11.itemId);
                v8.put("aggr_type", arg11.aggrType);
            } catch (JSONException v0) {
            }

            MobClickCombiner.onEvent(this.getActivity(), "push_history", arg10, arg11.groupId, v6, v8);
        }
    }

    public final void onResume() {
        long v6 = 0;
        super.onResume();
        int v0 = this.refreshList(-1, false);
        if (!this.mIsLoading && this.c < v6 && this.g != 1) {
            this.c = v6;
            this.a = true;
            this.queryData();
        }

        this.g = 0;
        this.setSelectionFromTop(v0);
    }

    public final void onScroll(AbsListView arg3, int arg4, int arg5, int arg6) {
        super.onScroll(arg3, arg4, arg5, arg6);
        if (this.mAdapter != null) {
            this.mAdapter.a(false);
        }

        if (!this.mIsLoading && !this.mData.isEmpty() && arg6 > 1 && arg6 == arg4 + arg5) {
            this.queryData();
        }
    }

    public final void onStop() {
        super.onStop();
        if (this.mNoDataView != null) {
            this.mNoDataView.b();
        }
    }

    public final void onViewScrollChanged(int arg3, int arg4, int arg5, int arg6) {
        super.onViewScrollChanged(arg3, arg4, arg5, arg6);
        if (this.mAdapter != null) {
            this.mAdapter.a(false);
        }
    }

    public final void queryData() {
        long v0;
        long v2 = 0;
        if (this.mData.isEmpty()) {
            this.a = true;
            if (this.e) {
                this.e = false;
            }
        }

        if ((this.a) || (this.mListData.c)) {
            if (!this.a) {
                v0 = !this.mData.isEmpty() ? this.mData.get(this.mData.size() - 1).cateBehotTime : v2;
                if (v0 > v2) {
                } else {
                    this.mListData.c = false;
                    this.mListData.d = false;
                    return;
                }
            } else {
                v0 = v2;
            }

            ++this.d;
            this.mIsLoading = true;
            this.hideNoDataView();
            if ((this.mData.isEmpty()) || (this.a)) {
                this.mFooter.hideFooterContent();
            } else {
                this.mFooter.switchToLoading();
            }

            if (this.a) {
                this.mPullRefreshList.e();
            }

            ArticleQueryObj v2_1 = new ArticleQueryObj(this.d, v0, 9);
            v2_1.historyType = "push";
            new ArticleQueryThread(this.mContext, this.f, v2_1).start();
        } else {
            this.mPullRefreshList.onRefreshComplete();
        }
    }

    public final void refreshListHook() {
        if (this.mData.isEmpty()) {
            this.showNoDataView();
        } else {
            this.hideNoDataView();
        }
    }
}

